﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MVCPets.Data;
using MVCPets.Extensions;
using MVCPets.Data.Model;

namespace MVCPets.Models
{
  public class ContentModel
  {
    #region Public Members
    public int? ID { get; set; }
    public string ContentName { get; set; }
    public string ContentText { get; set; }
    #endregion

    #region Constructors
    public ContentModel()
    {

    }

    public ContentModel(string key)
    {
			var rec = Data.DataAccess.DataStore.Query<Content>().Where(r => r.ContentName == key).FirstOrDefault();

			if (rec != null)
			{
				this.ID = rec.ID;
				this.ContentName = rec.ContentName;
				this.ContentText = rec.ContentText;
			}
    }
    #endregion

    #region Public Methods
    public bool Save()
    {
      var ret = false;

      if (this.ID.HasValue) ret = Update();
      else ret = Insert();

      return ret;
    }
    #endregion

    #region Private Methods
    private bool Update()
    {
			return Data.DataAccess.DataStore.UpdateObject(new Content()
				{
					ID = this.ID.Value,
					ContentName = this.ContentName,
					ContentText = this.ContentText
				});
    }

    private bool Insert()
    {
			return Data.DataAccess.DataStore.InsertObject(new Content()
				{
					ContentName = this.ContentName,
					ContentText = this.ContentText
				});
    }
    #endregion
  }
}